import { reqAddToCart, reqDetailList} from "@/api"
//引入uuidtoken
import getUUID from "@/utils/uuid_token"

const actions = {
  //获取商品详情页的数据
    async reqDetailList({commit},skuId){
      const result = await reqDetailList(skuId) 
      if(result.code == 200) {
          commit('REQDETAILLIST',result.data)
      }
    },

    //添加到购物车的请求----对已有的商品进行数量的改动
    async reqAddToCart({commit},{skuId,skuNum}){
        const result =await reqAddToCart(skuId,skuNum)
        if(result.code == 200){
          return Promise.resolve("ok")
        }else {
          return Promise.reject(new Error("fail"))
        }
    }
}
const mutations = {
  REQDETAILLIST(state,detailList){
    state.detailList = detailList
  }
}
const state = {
  detailList:{},
  uuidToken:getUUID()
}
const getters= {
  categoryView(state){
    return state.detailList.categoryView || {}
  },
  spuSaleAttrList(state){
    return state.detailList.spuSaleAttrList || []
  },
  skuInfo(state){
    return state.detailList.skuInfo || {}
  }
}

export default {
  namespaced:true,
  actions,
  mutations,
  state,
  getters
}